﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using Model;
using HT.DAL;

namespace BLL
{
    public class XskdhBLL
    {
        private readonly Dal_Xskdh_1 dal = new Dal_Xskdh_1();

        public int GetNewLsh()
        {
            int newLsh = 0;
            string maxLsh = GetDataByFiled("top 1 DjLsh", "xskdh", " order by DjLsh desc");
            if (maxLsh != "")
                newLsh = int.Parse(maxLsh);
            newLsh++;
            return newLsh;
        }
        public int GetNewLsh(string tableName)
        {
            int newLsh = 0;
            string maxLsh = GetDataByFiled("top 1 DjLsh", tableName, " order by DjLsh desc");
            if (maxLsh != "")
                newLsh = int.Parse(maxLsh);
            newLsh++;
            return newLsh;
        }

        public int GetNewLsh(string tableName, string filed)
        {
            int newLsh = 0;
            string maxLsh = GetDataByFiled("top 1 " + filed, tableName, " order by " + filed + " desc");
            if (maxLsh != "")
                newLsh = int.Parse(maxLsh);
            newLsh++;
            return newLsh;
        }

        /// <summary>
        /// 从商品档案表中获取信息
        /// </summary>
        /// <param name="spbh"></param>
        /// <param name="filed"></param>
        /// <returns></returns>
        public string GetDataByFiled(string spbh, string filed)
        {

            return dal.GetDataByFiled(spbh, filed);
        }
        public DataTable ExecuteDt(string sql)
        {
            return dal.ExecuteDt(sql);
        }

        /// <summary>
        /// 从 条件表中获取信息
        /// </summary>
        /// <param name="content">查询内容</param>
        /// <param name="tableName">表名</param>
        /// <param name="condition">条件(含 where 部分)</param>
        /// <returns></returns>
        public string GetDataByFiled(string content, string tableName, string condition)
        {
            return dal.GetDataByFiled(content, tableName, condition);
        }

        /// <summary>
        /// 从 条件表中获取信息
        /// </summary>

        /// <param name="filed">想获取值 字段</param>
        /// <param name="tableName">表名</param>
        /// <param name="key">条件</param>
        /// <param name="condition">条件值</param>
        /// <returns></returns>
        public string GetDataByFiled(string filed, string tableName, string key, string condition)
        {
            return dal.GetDataByFiled(filed, tableName, key, condition);
        }

        /// <summary>
        /// 获取最新 单据流水号
        /// </summary>
        /// <param name="type">0 销售</param>
        /// <returns></returns>
        public int GetNewDjlshID(int type)
        {
            return dal.GetNewDjlshID(type);
        }



        /// <summary>
        /// 获取最新 销售单编号
        /// </summary>
        /// <returns></returns>
        public string GetNewDJH(string Dmbh)
        {
            string newDJH = "";
            try
            {
                //select top 1 djlsh from dbo.xskdh order by djlsh desc
                string maxDJH = GetDataByFiled("djlsh", "stdjlsh", "djname", "YW_PFD");
                //   string maxDJH = GetDataByFiled(" top 1 djlsh", "xskdh", " order by djlsh desc ");
                if (maxDJH != "")
                {
                    if (maxDJH.Length < 8)
                    {
                        maxDJH = maxDJH.PadLeft(8, '0');
                    }
                    newDJH = Dmbh + maxDJH;
                }
            }
            catch { }

            return newDJH;
        }

        /// <summary>
        /// 获取 插入销售单 表头
        /// 创建：刘章明
        /// 时间：2012-08-30
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public string GetpfdHStringBuilder(xskdh model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into xskdh(");
            strSql.Append("DjLsh,DjState,rq,sj,djzt,xsdh,Bmbh,Bmmc,yyybh,yyymc,khbh,khmc,hyk,hykxf,slhj,jehj,xj,zrje,bqjf,Zjf,bz,djyh,djsj,xgyh,xgsj,yl,ssjehj,wx,zfb,djq,yhk)");
            strSql.Append(" values (");
            strSql.Append(model.DjLsh + "," + model.DjState + ",'" + model.rq + "','" + model.sj + "','" + model.djzt + "','" + model.xsdh + "','" + model.Bmbh + "','" + model.Bmmc + "','" + model.yyybh + "','" + model.yyymc + "','" + model.khbh + "','" + model.khmc + "','" + model.hyk + "'," + model.hykxf + "," + model.slhj + "," + model.jehj + "," + model.xj + "," + model.zrje + "," + model.bqjf + "," + model.Zjf + ",'" + model.bz + "','" + model.djyh + "','" + model.djsj + "','" + model.xgyh + "','" + model.xgsj + "'," + model.yl + "," + model.ssjehj + "," + model.wx + "," + model.zfb + "," + model.djq + "," + model.yhk + ")");

            return strSql.ToString();
        }




        /// <summary>
        /// 获取 更新最后流水号字符串
        /// 创建：刘章明
        /// 时间：2012-08-30
        /// </summary>
        /// <returns></returns>
        public string GetStdjlshStringBuilder()
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update stdjlsh set djlsh=djlsh+1 where djname='YW_PFD'");
            return strSql.ToString();
        }

        /// <summary>
        /// 获取 插入销售单 表体
        /// 创建：刘章明
        /// 时间：2012-08-30
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public string GetpfdBStringBuilder(xskdB model)
        {

            if (model.kcsl == null || model.kcsl.ToString() == "")
                model.kcsl = 0;
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into xskdB(");
            strSql.Append("DjLsh,DjBth,DjState,hh,tmh,spbh,spmc,gg,dw,sl,Dj,pfj,hyzk,hyyh,zyhjg,kcsl,je,hxsdh,yyy,jj,ysbh,ysmc,cm)");
            strSql.Append(" values ( ");
            strSql.Append(model.DjLsh + "," + model.DjBth + ",'" + model.DjState + "'," + model.hh + ", '" + model.tmh + "','" + model.spbh + "','" + model.spmc + "','" + model.gg + "','" + model.dw + "'," + model.sl + "," + model.lsj + "," + model.pfj + "," + model.zkl + "," + model.hyyh + "," + model.zyhjg + "," + model.kcsl + "," + model.ssje + ",'" + model.hxsdh + "','" + model.yyy + "'," + model.jj + ",'" + model.ysbh + "','" + model.ysmc + "','" + model.cm + "')");
            return strSql.ToString();
        }

        /// <summary>
        /// 会员卡信息修改
        /// </summary>
        /// <param name="xskdhModel"></param>
        /// <returns></returns>
        public string GetUpdateHykblHStringBuilder(xskdh xskdhModel)
        {
            string sql = "";
            if (xskdhModel.Hykye != "" && xskdhModel.Zjf.ToString() != "" && xskdhModel.khbh != null && xskdhModel.khbh != "")
            {
                string time = DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss");
                sql = "update hykblH set hykye=" + xskdhModel.Hykye + ",jfye=" + xskdhModel.Zjf + ",zjxfjf=" + xskdhModel.Zjxfjf + ",zjxfsj='" + time + "',indexCount=indexCount+1  where kh='" + xskdhModel.khbh + "'";
            }
            return sql;
        }



        /// <summary>
        /// 保存销售单
        /// </summary>
        /// <param name="sqlList"></param>
        /// <returns></returns>
        public int SaveXskdData(List<string> sqlList)
        {
            return dal.SaveXskdData(sqlList);
        }



        /// <summary>
        /// 得到一个对象实体(xskdh)
        /// </summary>
        /// <returns></returns>
        public xskdh GetXskdhModel(string xsdh)
        {
            return dal.GetXskdhModel(xsdh);
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataTable GetPfdBData(string xsdh)
        {
            return dal.GetPfdBData(xsdh);
        }

    }
}
